﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using HadronData;

namespace Hadron.SpecialEffects
{
    class Explosion
        : SpecialEffect
    {
        private Environment m_environment;
        private ParticleSystem m_particleSystem;
        private float m_fInitialLifetime;
        private float m_fSize;

        public Explosion(Environment p_environment, Vector2 p_vecPosition, float p_fSize, float p_fLiftime)
        {
            m_environment = p_environment;

            m_fSize = p_fSize;
            m_fInitialLifetime = p_fLiftime;
            int nParticleCount = (int)(128 * p_fSize);
            m_particleSystem = new ParticleSystem(nParticleCount, 0.0f, p_fLiftime);
            m_particleSystem.Position = p_vecPosition;
            m_particleSystem.ParticleColours = new Color[] { Color.White, Color.Yellow, Color.Red, Color.Black};
            m_particleSystem.EmissionVelocityVariance = 48.0f * p_fSize;
            m_particleSystem.EmitBatch(nParticleCount);

            m_fLifetime = p_fLiftime;

            float fSoundVolume = m_environment.ComputeSoundVolume(p_vecPosition);
            if (p_fSize <= 1.0f)
                AssetManager.Sounds.EnemyExplosion.Play();
            else
                AssetManager.Sounds.PlayerExplosion.Play();
        }

        public override void Update(GameTime p_gameTime)
        {
            m_particleSystem.Update(p_gameTime);

            float fDeltaTime = (float)p_gameTime.ElapsedGameTime.TotalSeconds;
            m_fLifetime -= fDeltaTime;
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch p_spriteBatch)
        {
            m_particleSystem.Draw(p_spriteBatch);

            float fIntensity = m_fLifetime / m_fInitialLifetime;
            fIntensity *= fIntensity;
            Color color = new Color(fIntensity, fIntensity * 0.1f, 0.0f);
            p_spriteBatch.Draw(AssetManager.Textures.Ring, m_particleSystem.Position, null, color, 0.0f, 
                new Vector2(64.0f, 64.0f), (1.0f - fIntensity) * 1.5f * m_fSize, SpriteEffects.None, 0.0f); 
        }
    }
}
